package com.ating.demo.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ating.demo.Entity.Employee;
import com.ating.demo.Service.EmployeeService;
import com.ating.demo.common.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Array;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**查询所有**/
    @GetMapping("/getAll")
    public Result getEmployee(Employee employee){
        List<Employee> list = employeeService.selectAll(employee);
        return Result.success(list);
    }

    /**查询当个**/
    @GetMapping("/getOne/{id}")
    public Result getOne(@PathVariable Integer id){
        Employee employee = employeeService.selectOne(id);
        return Result.success(employee);
    }

    /**分页查询**/
    @GetMapping("/selectPage")
    public Result getPage(@RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          Employee employee){
        PageInfo<Employee> pageInfo = employeeService.selectPage(pageSize,pageNum,employee);
        return Result.success(pageInfo);
    }

    /**添加用户**/
    @PostMapping("/add")
    public Result add(@RequestBody  Employee employee){
        employeeService.add(employee);
        return Result.success();
    }

    /**更新用户**/
    @PutMapping("/update")
    public Result update(@RequestBody  Employee employee){
        employeeService.update(employee);
        return Result.success();
    }

    /**删除**/
    @DeleteMapping("/delete/{id}")
    public Result move(@PathVariable Integer id){
        employeeService.moveById(id);
        return Result.success();
    }

    /**批量删除**/
    @DeleteMapping("/batch")
    public Result batch(@RequestBody List<Integer> ids){
        for(Integer id : ids){
            employeeService.moveById(id);
        }
        return Result.success();
    }

    /**导出数据**/
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Employee> list = employeeService.selectAll(null);
        //写到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //设置中文表头
        writer.addHeaderAlias("username","账号");
        writer.addHeaderAlias("name","姓名");
        writer.addHeaderAlias("sex","性别");
        writer.addHeaderAlias("no","工号");
        writer.addHeaderAlias("description","描述");
        writer.addHeaderAlias("descriptionName","部门");
        //设置想导出的字段
        writer.setOnlyAlias(true);
        //写出数据
        writer.write(list,true);
        //设置输出文件名、文件头和格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String filename = URLEncoder.encode("员工信息",StandardCharsets.UTF_8);
        response.setHeader("Content-Disposition", "attachment;filename="+filename+".xlsx");
        //写出到输出流
        OutputStream os = response.getOutputStream();
        writer.flush(os);
        writer.close();
    }

    @PostMapping("/import")
    public Result importExcel(MultipartFile file) throws IOException {
        //输入文件
        InputStream is = file.getInputStream();
        //读文件
        ExcelReader reader = ExcelUtil.getReader(is);
        //读取里面的数据
        reader.addHeaderAlias("账号","username");
        reader.addHeaderAlias("姓名","name");
        reader.addHeaderAlias("性别","sex");
        reader.addHeaderAlias("工号","no");
        reader.addHeaderAlias("描述","description");
        reader.addHeaderAlias("部门","descriptionName");
        List<Employee> list = reader.readAll(Employee.class);
        //写入数据库
        for (Employee employee : list) {
            employeeService.add(employee);
        }
        return Result.success();
    }

}
